#include <sstream>
#include <sys/time.h>

#include "lib.h"

namespace utils
{

int StringToNum( const std::string& sInput ) throw()
{
	std::stringstream ss;
	ss << sInput;
	int nValue;
	ss >> nValue;
//	if( -1 == nValue )
//	{
//		throw utils::Exception( "Invalid value in StringToNum function! Cannot get fn attribute" );
//	}

	return nValue;
}

double StringToDouble( const std::string& sInput ) throw()
{
	std::stringstream ss;
	ss << sInput;
	double dValue;
	ss >> dValue;
//	if( -1 == dValue )
//	{
//		throw utils::Exception( "Invalid value in StringToDouble function! Cannot get mark's attribute" );
//	}
	return dValue;
}

std::string IntToString( const int& nInput ) throw()
{
	std::string s;
	std::stringstream out;
	out << nInput;
	s = out.str();
	return s;
}

std::string GetCurrentTimeString() throw()
{
	std::stringstream ssTime;

	timeval t;
	gettimeofday(&t, NULL);
	std::string buf(40, '\0');
	strftime(&buf[0], buf.size(), " %H:%M:%S:", localtime(&t.tv_sec));

	ssTime << buf << t.tv_usec/1000;

	return ssTime.str();
}

void GetXMLNodeSetAtPath(
    TiXmlNode* pRoot,
    const std::string& sXPath,
    XMLNodeSet& vecResult ) throw( std::exception )
{
    if( NULL == pRoot )
    {
        throw Exception( "Canno calculate node set from NULL tree" );
    }

    TinyXPath::xpath_processor xp_proc( pRoot, sXPath.c_str() );
    unsigned unNodesCount = xp_proc.u_compute_xpath_node_set();

    for( unsigned unIdx = 0; unIdx < unNodesCount; ++unIdx )
    {
        const TiXmlBase* pXMLBase;
        bool bIsAttribute;
        xp_proc.v_get_xpath_base( unIdx, pXMLBase, bIsAttribute );
        vecResult.push_back( std::make_pair( const_cast<TiXmlBase*>(pXMLBase), bIsAttribute ) );
    }
}

} // namespace utils
